﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using BUS;
using DTO;

namespace GUI
{
    public partial class ucExport : UserControl
    {
        public ucExport()
        {
            InitializeComponent();
        }        

        private void btnXuatFile_Click(object sender, EventArgs e)
        {
            Excel.Application application = new Excel.Application();
            Excel.Workbook workbook = application.Workbooks.Add();
            Excel.Worksheet worksheet = workbook.Sheets[1];

            DataTable dataTable = new DataTable();

            if (radiopin.Checked == true)
            {
                dataTable = ThietBiDienBUS.LayDanhSachPIN();
                var columns = dataTable.Columns.Count;
                var rows = dataTable.Rows.Count;
                Excel.Range range = worksheet.Range["A1", String.Format("{0}{1}", GetExcelColumnName(columns), rows)];
                object[,] data = new object[rows, columns];
                data[0, 0] = "Tên thiết bị";
                data[0, 1] = "Loại thiết bị";
                data[0, 2] = "Giá";
                data[0, 3] = "Công xuất";
                data[0, 4] = "Kích thước";
                data[0, 5] = "Imp";
                data[0, 6] = "Vmp";
                data[0, 7] = "Isc";
                data[0, 8] = "Voc";
                for (int rowNumber = 1; rowNumber < rows; rowNumber++)
                {
                    for (int columnNumber = 0; columnNumber < columns - 1; columnNumber++)
                    {
                        data[rowNumber, columnNumber] = dataTable.Rows[rowNumber][columnNumber].ToString();
                    }
                }
                range.Value = data;
            }
            if (radioBKD.Checked == true)
            {
                dataTable = ThietBiDienBUS.LayDanhSachBOKICHDIEN();
                var columns = dataTable.Columns.Count;
                var rows = dataTable.Rows.Count;
                Excel.Range range = worksheet.Range["A1", String.Format("{0}{1}", GetExcelColumnName(columns), rows)];
                object[,] data = new object[rows, columns];
                data[0, 0] = "Tên thiết bị";
                data[0, 1] = "Loại thiết bị";
                data[0, 2] = "Giá";
                data[0, 3] = "Công xuất";
                for (int rowNumber = 1; rowNumber < rows; rowNumber++)
                {
                    for (int columnNumber = 0; columnNumber < columns - 1; columnNumber++)
                    {
                        data[rowNumber, columnNumber] = dataTable.Rows[rowNumber][columnNumber].ToString();
                    }
                }
                range.Value = data;
            }
            if (radioAcquy.Checked == true)
            {
                dataTable = ThietBiDienBUS.LayDanhSachACQUY();
                var columns = dataTable.Columns.Count;
                var rows = dataTable.Rows.Count;
                Excel.Range range = worksheet.Range["A1", String.Format("{0}{1}", GetExcelColumnName(columns), rows)];
                object[,] data = new object[rows, columns];
                data[0, 0] = "Tên thiết bị";
                data[0, 1] = "Loại thiết bị";
                data[0, 2] = "Giá";
                data[0, 3] = "Công xuất";
                data[0, 4] = "Kích thước";
                data[0, 5] = "Imp";
                data[0, 6] = "Vmp";
                for (int rowNumber = 1; rowNumber < rows; rowNumber++)
                {
                    for (int columnNumber = 0; columnNumber < columns - 1; columnNumber++)
                    {
                        data[rowNumber, columnNumber] = dataTable.Rows[rowNumber][columnNumber].ToString();
                    }
                }
                range.Value = data;
            }
            if (radioBoSac.Checked == true)
            {
                dataTable = ThietBiDienBUS.LayDanhSachBOSAC();
                var columns = dataTable.Columns.Count;
                var rows = dataTable.Rows.Count;
                Excel.Range range = worksheet.Range["A1", String.Format("{0}{1}", GetExcelColumnName(columns), rows)];
                object[,] data = new object[rows, columns];
                data[0, 0] = "Tên thiết bị";
                data[0, 1] = "Loại thiết bị";
                data[0, 2] = "Giá";
                data[0, 3] = "Công xuất";
                for (int rowNumber = 1; rowNumber < rows; rowNumber++)
                {
                    for (int columnNumber = 0; columnNumber < columns - 1; columnNumber++)
                    {
                        data[rowNumber, columnNumber] = dataTable.Rows[rowNumber][columnNumber].ToString();
                    }
                }
                range.Value = data;
            }
            application.Visible = true;
            Marshal.ReleaseComObject(application);

         }

        private void CreateIfMissing(string path)
        {
            bool folderExists = Directory.Exists(path);
            if (!folderExists)
                Directory.CreateDirectory(path);
        }

        private static string GetExcelColumnName(int columnNumber)
        {
            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;

            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }

            return columnName;
        }

        public void CreateCSVFile(DataTable dt, string strFilePath)
        {
            try
            {
                // Create the CSV file to which grid data will be exported.
                StreamWriter sw = new StreamWriter(strFilePath, false);
                // First we will write the headers.
                //DataTable dt = m_dsProducts.Tables[0];
                int iColCount = dt.Columns.Count;
                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write(dt.Columns[i]);
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);

                // Now write all the rows.

                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < iColCount; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            sw.Write(dr[i].ToString());
                        }
                        if (i < iColCount - 1)
                        {
                            sw.Write(",");
                        }
                    }

                    sw.Write(sw.NewLine);
                }
                sw.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

       
        
    }
}
